package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.entity.ParkingFeeConfig;
import com.example.entity.dto.ParkingFeeConfigDTO;
import com.example.mapper.ParkingFeeConfigMapper;
import com.example.service.ParkingFeeConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ParkingFeeConfigServiceImpl extends ServiceImpl<ParkingFeeConfigMapper, ParkingFeeConfig> implements ParkingFeeConfigService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createConfig(ParkingFeeConfigDTO dto) {
        // 检查配置名称是否已存在
        LambdaQueryWrapper<ParkingFeeConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ParkingFeeConfig::getConfigName, dto.getConfigName());
        if (count(queryWrapper) > 0) {
            throw new RuntimeException("配置名称已存在");
        }

        ParkingFeeConfig config = ParkingFeeConfig.builder().build();
        BeanUtils.copyProperties(dto, config);
        config.setCreateTime(LocalDateTime.now());
        config.setUpdateTime(LocalDateTime.now());
        // 设置默认状态为启用
        if (config.getStatus() == null) {
            config.setStatus(1);
        }

        return save(config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfig(Long id, ParkingFeeConfigDTO dto) {
        ParkingFeeConfig config = getById(id);
        if (config == null) {
            throw new RuntimeException("配置不存在");
        }

        // 检查配置名称是否已存在(排除自身)
        if (dto.getConfigName() != null && !dto.getConfigName().equals(config.getConfigName())) {
            LambdaQueryWrapper<ParkingFeeConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ParkingFeeConfig::getConfigName, dto.getConfigName())
                    .ne(ParkingFeeConfig::getId, id);
            if (count(queryWrapper) > 0) {
                throw new RuntimeException("配置名称已存在");
            }
        }

        BeanUtils.copyProperties(dto, config);
        config.setId(id);
        config.setUpdateTime(LocalDateTime.now());

        return updateById(config);
    }

    @Override
    public ParkingFeeConfigDTO getConfig(Long id) {
        ParkingFeeConfig config = getById(id);
        if (config == null) {
            throw new RuntimeException("配置不存在");
        }

        ParkingFeeConfigDTO dto = new ParkingFeeConfigDTO();
        BeanUtils.copyProperties(config, dto);
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfig(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeStatus(Long id, Integer status) {
        ParkingFeeConfig config = getById(id);
        if (config == null) {
            throw new RuntimeException("配置不存在");
        }

        config.setStatus(status);
        config.setUpdateTime(LocalDateTime.now());
        return updateById(config);
    }
}
